package com.htrq.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import com.htrq.entity.Page;
import com.htrq.entity.User;
import com.htrq.service.UserService;

@Controller
@RequestMapping("jsp/sys")
public class UserController {
	@Resource
	private UserService userService;

	@RequestMapping("getAlluser")
	@ResponseBody
	public String getAlluser(User user, Page page, HttpServletResponse response)
			throws IOException {
		List<User> list=userService.getAlluser(user, page);
		String str = JSON.toJSONString(list);
		Long count = userService.getTotal();
		String str1 = "{total:" + count + ",users:" + str + "}";
		response.setContentType("text/html;charset=utf8");
		PrintWriter out = response.getWriter();
		out.print(str1);
		return null;
	}
	
	/**
	 *form表单提交返回需要特定数据格式
	 * String str="{'success':true,'res':{'result':'添加成功'}}";
	 */
	@RequestMapping("saveuser")
	@ResponseBody
	public String saveuser(User user,HttpServletResponse response) throws IOException {
		boolean flag= userService.saveuser(user);
		response.setContentType("text/html;charset=utf8");
		PrintWriter out = response.getWriter();
		if(flag){
			out.print("{'success':true,'res':{'result':'添加成功'}}");
		}else{
			out.print("{'success':true,'res':{'result':'添加失败'}}");
		}
		return null;
	}
}
